package com.csjung.games.gamenori.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;

import com.csjung.games.gamenori.shared.GamenoriRequestFactory;
import com.csjung.games.gamenori.shared.UserProxy;

public class UserInfoPopup extends PopupPanel {

  private UserProxy userProxy;

  public UserProxy getUserProxy() {
    return userProxy;
  }

  public void setUserProxy(UserProxy userProxy) {
    this.userProxy = userProxy;
  }

  private final GamenoriRequestFactory requestFactory;

  private Button chatButton;

  public UserInfoPopup(UserProxy userProxy, GamenoriRequestFactory requestFactory) {
    super(true);
    this.userProxy = userProxy;
    this.requestFactory = requestFactory;
    chatButton = new Button("Chatting");
    chatButton.setWidth("11ex");
    HorizontalPanel hPanel = new HorizontalPanel();
    hPanel.setWidth("200px");
    hPanel.add(chatButton);
    
    hPanel.addDomHandler(new MouseOutHandler() {
      public void onMouseOut(MouseOutEvent event) {
        hide();
      }
    }, MouseOutEvent.getType());
    setWidget(hPanel);
  }

  @Override
  protected void onLoad() {
    super.onLoad();
    chatButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
        ChatDialogBox chatBox = new ChatDialogBox(requestFactory);
        chatBox.setAnimationEnabled(true);
        chatBox.setText(userProxy.getNickname());
        chatBox.createChannel(userProxy.getUserId());
        chatBox.center();
        chatBox.show();
      };
    });
  }
}
